package cn.group.controller;

import cn.group.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import pojo.entity.User;
import utils.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "用户信息")
@RestController
@RequestMapping(value = "/user", produces = "text/html;charset=UTF-8")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @ApiOperation("查询全部用户")
    @RequestMapping("/findAllUser1.do")
    @ResponseBody
    public String findAllUser1(HttpServletResponse response) {
        List<User> allUser = userService.findAllUser1();
        String allUserJson = writerSuccessResult(allUser, response);
        return allUserJson;
    }

    @ApiOperation("查询全部用户")
    @RequestMapping("/findAllUser2.do")
    @ResponseBody
    public String findAllUser2(HttpServletResponse response) {
        List<User> allUser = userService.findAllUser2();
        String allUserJson = writerSuccessResult(allUser, response);
        return allUserJson;
    }

    @ApiOperation("删除用户")
    @RequestMapping("/removeUser.do")
    @ResponseBody
    public String removeUser(Long id, HttpServletResponse response) {
        int i = userService.removeUser(id);
        String result = writerSuccessResult(i, response);
        return result;
    }

    @ApiOperation("更新用户")
    @RequestMapping("/updateUser.do")
    @ResponseBody
    public String updateUser(User user, HttpServletResponse response) {
        int i = userService.updateUser(user);
        String result = writerSuccessResult(i, response);
        return result;
    }

    @ApiOperation("添加用户")
    @RequestMapping("/addUser.do")
    @ResponseBody
    public String addUser(User user, HttpServletResponse response) {
        int i = userService.addUser(user);
        String result = writerSuccessResult(i, response);
        return result;
    }

    @ApiOperation("根据id查询用户")
    @RequestMapping("/findUserById.do")
    @ResponseBody
    public String findUserById(Long userId,HttpServletResponse response) {
        User user = userService.findUserById(userId);
        return writerSuccessResult(user,response);
    }

    @ApiOperation("根据用户名查询用户")
    @RequestMapping("/findUserByName.do")
    @ResponseBody
    public String findUserByName(String userName, HttpServletResponse response){
        List<User> user = userService.findUserByName(userName);
        String result = writerSuccessResult(user, response);
        return result;
    }
}
